/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frmwrk.core;

import frmwrk.interfaces.IAccount;
import frmwrk.interfaces.IEntry;
import frmwrk.interfaces.IParty;
import java.util.HashMap;

/**
 *
 * @author Damian
 */
public class FinancialFramework {
    
    HashMap<Integer, IAccount> accountMap = new HashMap<Integer, IAccount>();
    HashMap<String, IParty> partyMap = new HashMap<String, IParty>();
    
    public boolean isNewParty(String name){
        if (partyMap.get(name) == null)
            return true;
        return false;
    }
    
    public boolean addAccountEntry(int accountNo, IEntry entry){
        IAccount account = getAccount(accountNo);
        entry.setPreviousBalance(account.getBalance());
        return account.addEntry(entry);
    }
    
    public IAccount getAccount(int accountNumber){
        return accountMap.get(accountNumber);
                
    }
            
    public void addToAccountMap(IAccount acc){
        
        accountMap.put(acc.getAccountNumber(), acc);
    }
    
    public void addToPartyMap(IParty party){
        
        partyMap.put(party.getName(), party);
    }
    
    public void createAccount(APartyFactory partyFactory, HashMap partyParams, AAccountFactory accountFactory, HashMap accParams){
        
        IParty party;
        if (isNewParty(partyParams.get("partyName").toString())){            
            party = partyFactory.create(partyParams);
            addToPartyMap(party);
        } else {
            party = partyMap.get(partyParams.get("partyName").toString());
        }
        
        Account acc = accountFactory.create(accParams);
        acc.setParty(party);
        addToAccountMap(acc);
        acc.notifyObservers();
    }
    
}
